<script setup>
import { reactive, ref } from 'vue'
import { useRouter } from "vue-router";
import { ElMessage } from 'element-plus'
import { Plus } from '@element-plus/icons-vue'

const showAlert = ref(false)
const dialogVisible = ref(false)

// do not use same name with ref
const form = reactive({
  name: '',
  region: '',
  date1: '',
  date2: '',
  delivery: false,
  type: [],
  resource: '',
  desc: '',
})

const router = useRouter();

const onSubmit = () => {
  console.log('submit!')
  dialogVisible.value = true
}

const jumpintoUserList = () => {
  router.push({ path: '/user/list' })
}

const showdialog1 = () => {
  dialogVisible.value = false
  showAlert.value = true
}

const imageUrl = ref('')

const handleAvatarSuccess = (response, uploadFile) => {
  imageUrl.value = URL.createObjectURL(uploadFile.raw)
}

const beforeAvatarUpload = (rawFile) => {
  if (rawFile.type !== 'image/jpeg') {
    ElMessage.error('Avatar picture must be JPG format!')
    return false
  } else if (rawFile.size / 1024 / 1024 > 2) {
    ElMessage.error('Avatar picture size can not exceed 2MB!')
    return false
  }
  return true
}
</script>

<template>
  <el-card class="page-container">
    <template #header>
      <div>
        <el-form :model="form" label-width="auto" style="max-width: 600px">
          <el-form-item label="名称">
            <el-input v-model="form.name" />
          </el-form-item>
          <el-form-item label="身份">
            <el-select v-model="form.region">
              <el-option label="Zone one" value="shanghai" />
              <el-option label="Zone two" value="beijing" />
            </el-select>
          </el-form-item>
          <el-form-item label="时间">
            <el-col :span="11">
              <el-date-picker v-model="form.date1" type="date" placeholder="请选择" style="width: 100%" />
            </el-col>
            <el-col :span="2" class="text-center">
              <span class="text-gray-500">-</span>
            </el-col>
            <el-col :span="11">
              <el-time-picker v-model="form.date2" placeholder="请选择" style="width: 100%" />
            </el-col>
          </el-form-item>
          <el-form-item label="开关">
            <el-switch v-model="form.delivery" />
          </el-form-item>
          <el-form-item label="？？？？">
            <el-checkbox-group v-model="form.type">
              <el-checkbox value="Online activities" name="type">
                111
              </el-checkbox>
              <el-checkbox value="Promotion activities" name="type">
                222
              </el-checkbox>
              <el-checkbox value="Offline activities" name="type">
                333
              </el-checkbox>
              <el-checkbox value="Simple brand exposure" name="type">
                444
              </el-checkbox>
            </el-checkbox-group>
          </el-form-item>
          <el-form-item label="xxxx">
            <el-radio-group v-model="form.resource">
              <el-radio value="Sponsor">yyyy</el-radio>
              <el-radio value="Venue">zzzz</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="个人签名">
            <el-input v-model="form.desc" type="textarea" />
          </el-form-item>

          <el-upload class="avatar-uploader" action="https://run.mocky.io/v3/9d059bf9-4660-45f2-925d-ce80ad6c4d15"
            :show-file-list="false" :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload">
            <img v-if="imageUrl" :src="imageUrl" class="avatar" />
            <el-icon v-else class="avatar-uploader-icon">
              <Plus />
            </el-icon>
          </el-upload>

          <el-form-item>
            <el-button type="primary" @click="onSubmit">修改</el-button>
            <el-button>取消</el-button>
          </el-form-item>
        </el-form>
      </div>
    </template>
  </el-card>
  <el-alert class="center-alert" v-if="showAlert" title="成功修改" type="success" show-icon />
  <el-dialog v-model="dialogVisible" title="提示" width="500" :before-close="handleClose">
    <span>您确认要修改个人信息吗？</span>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="showdialog1">
          确定修改
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>

//头像
<style scoped>
.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}
</style>
